package com.holly.unit.deform.api.factory;

import cn.hutool.core.util.StrUtil;
import com.holly.unit.deform.api.pojo.base.DictModelDTO;

import java.util.List;
import java.util.Map;

/**
 * 类描述: 预知型值转换
 * 提前知道了当前数据可能需要替换的所有字典值
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/14 14:42
 */
public class ForeseeConvert implements FieldCommentConverter{

    protected String model;

    protected List<DictModelDTO> dictList;

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public List<DictModelDTO> getDictList() {
        return dictList;
    }

    public void setDictList(List<DictModelDTO> dictList) {
        this.dictList = dictList;
    }


    @Override
    public Object converterToVal(String txt) {
        if (StrUtil.isNotEmpty(txt)) {
            for (DictModelDTO model : this.dictList) {
                if (model.getText().equals(txt)) {
                    return model.getValue();
                }
            }
        }
        return txt;
    }

    @Override
    public Object converterToTxt(String val) {
        if (StrUtil.isNotEmpty(val)) {
            for (DictModelDTO model : this.dictList) {
                if (model.getValue().equals(val)) {
                    return model.getText();
                }
            }
        }
        return val;
    }

    @Override
    public Map<String, String> getConfig() {
        return null;
    }

}
